﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Navigation;
using GalaSoft.MvvmLight.Command;
using Microsoft.Phone.Controls;
using WPNEXT.Common;
using WPNEXT.Model;

namespace WPNEXT.ViewModel
{
    public class ExplodeViewModel : BaseViewModel
    {
        #region Fields
        private ObservableCollection<ImageModel> _items = new ObservableCollection<ImageModel>();
        private bool _loading;

        private RelayCommand _getDataCommand;
        private RelayCommand _addToTopCommand;
        private RelayCommand _refreshCommand;
        private RelayCommand<object> _explodItemRemoveCommand;
        private RelayCommand _setToNullCommand;
        private RelayCommand<object> _itemTapCommand;
        private RelayCommand _deleteSelectCommand;
        private RelayCommand _resetCommand;
        #endregion

        #region Properties
        public ObservableCollection<ImageModel> Items
        {
            get { return _items; }
            set
            {
                if (value != _items)
                {
                    _items = value;
                    this.RaisePropertyChanged(() => this.Items);
                }
            }
        }

        public bool Loading
        {
            get { return _loading; }
            set
            {
                if (value != _loading)
                {
                    _loading = value;
                    this.RaisePropertyChanged(() => this.Loading);
                }
            }
        }
        #endregion

        #region Command
        public RelayCommand GetDataCommand
        {
            get
            {
                if (_getDataCommand == null)
                {
                    _getDataCommand = new RelayCommand(GetItems);
                }
                return _getDataCommand;
            }
            private set { _getDataCommand = value; }
        }

        public RelayCommand AddToTopCommand
        {
            get
            {
                if (_addToTopCommand == null)
                {
                    _addToTopCommand = new RelayCommand(AddToTop);
                }
                return _addToTopCommand;
            }
            private set { _addToTopCommand = value; }
        }

        public RelayCommand RefreshCommand
        {
            get
            {
                if (_refreshCommand == null)
                {
                    _refreshCommand = new RelayCommand(RefreshExecute);
                }
                return _refreshCommand;
            }
            private set { _refreshCommand = value; }
        }

        public RelayCommand<object> ExplodItemRemoveCommand
        {
            get
            {
                if (_explodItemRemoveCommand == null)
                {
                    _explodItemRemoveCommand = new RelayCommand<object>(ExplodRemoveItem);
                }
                return _explodItemRemoveCommand;
            }
            private set { _explodItemRemoveCommand = value; }
        }

        public RelayCommand SetToNullCommand
        {
            get
            {
                if (_setToNullCommand == null)
                {
                    _setToNullCommand = new RelayCommand(SetToNullExecute);
                }
                return _setToNullCommand;
            }
            private set { _setToNullCommand = value; }
        }

        public RelayCommand<object> ItemTapCommand
        {
            get
            {
                if (_itemTapCommand == null)
                {
                    _itemTapCommand = new RelayCommand<object>(ItemTapExecute);
                }
                return _itemTapCommand;
            }
            private set { _itemTapCommand = value; }
        }

        public RelayCommand DeleteSelectCommand
        {
            get
            {
                if (_deleteSelectCommand == null)
                {
                    _deleteSelectCommand = new RelayCommand(DeleteSelectExecute);
                }
                return _deleteSelectCommand;
            }
            private set { _deleteSelectCommand = value; }
        }

        public RelayCommand ResetCommand
        {
            get
            {
                if (_resetCommand == null)
                {
                    _resetCommand = new RelayCommand(ResetExecute);
                }
                return _resetCommand;
            }
            private set { _resetCommand = value; }
        }

        #endregion

        #region Method
        private void GetItems()
        {
            if (!Loading)
            {
                Loading = true;
                List<ImageModel> list = new List<ImageModel>();
                list.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao15866793897_146x219.jpg" });
                list.Add(new ImageModel { ImgRatio = 1.33, Url = "http://img.geilicdn.com/taobao19765708787_146x194.jpg" });
                list.Add(new ImageModel { ImgRatio = 1.00, Url = "http://img.geilicdn.com/taobao15848113020_146x146.jpg" });
                list.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao15848325228_146x219.jpg" });
                list.Add(new ImageModel { ImgRatio = 1.03, Url = "http://img.geilicdn.com/taobao19779004225_146x150.jpg" });
                list.Add(new ImageModel { ImgRatio = 1.57, Url = "http://img.geilicdn.com/taobao15865634518_146x229.jpg" });
                list.Add(new ImageModel { ImgRatio = 0.99, Url = "http://img.geilicdn.com/taobao15865434962_146x144.jpg" });
                list.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao15830113145_146x219.jpg" });
                list.Add(new ImageModel { ImgRatio = 1.15, Url = "http://img.geilicdn.com/taobao17077543430_146x167.jpg" });
                list.Add(new ImageModel { ImgRatio = 1.46, Url = "http://img.geilicdn.com/taobao19726272617_146x213.jpg" });
                list.Add(new ImageModel { ImgRatio = 1.12, Url = "http://img.geilicdn.com/taobao15865557654_146x163.jpg" });
                list.Add(new ImageModel { ImgRatio = 1.38, Url = "http://img.geilicdn.com/taobao19776424457_146x201.jpg" });
                list.Add(new ImageModel { ImgRatio = 0.99, Url = "http://img.geilicdn.com/taobao19712332041_146x144.jpg" });
                list.Add(new ImageModel { ImgRatio = 1.00, Url = "http://img.geilicdn.com/taobao19722332410_146x146.jpg" });
                list.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao15851750545_146x219.jpg" });
                list.Add(new ImageModel { ImgRatio = 1.35, Url = "http://img.geilicdn.com/taobao15857918167_146x197.jpg" });
                list.Add(new ImageModel { ImgRatio = 1.53, Url = "http://img.geilicdn.com/taobao19704356816_146x223.jpg" });
                list.Add(new ImageModel { ImgRatio = 1.33, Url = "http://img.geilicdn.com/taobao19725572516_146x194.jpg" });
                list.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao19725916030_146x219.jpg" });
                list.Add(new ImageModel { ImgRatio = 1.60, Url = "http://img.geilicdn.com/taobao15845742605_146x233.jpg" });
                list.Add(new ImageModel { ImgRatio = 1.41, Url = "http://img.geilicdn.com/taobao15842817755_146x205.jpg" });
                list.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao15835866335_146x219.jpg" });
                list.Add(new ImageModel { ImgRatio = 1.53, Url = "http://img.geilicdn.com/taobao15693786979_146x223.jpg" });
                list.Add(new ImageModel { ImgRatio = 1.33, Url = "http://img.geilicdn.com/taobao17072171509_146x194.jpg" });
                list.Add(new ImageModel { ImgRatio = 0.67, Url = "http://img.geilicdn.com/taobao17072151693_146x97.jpg" });
                list.Add(new ImageModel { ImgRatio = 1.51, Url = "http://img.geilicdn.com/taobao15882494639_146x220.jpg" });
                list.Add(new ImageModel { ImgRatio = 1.49, Url = "http://img.geilicdn.com/taobao19813600740_146x217.jpg" });
                list.ToObservableCollection<ImageModel>(this.Items);
                //Thread.Sleep(5000);
                Loading = false;
            }
        }

        private void FirstGetItems()
        {
            List<ImageModel> list = new List<ImageModel>();
            list.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao15866793897_146x219.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.33, Url = "http://img.geilicdn.com/taobao19765708787_146x194.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.00, Url = "http://img.geilicdn.com/taobao15848113020_146x146.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao15848325228_146x219.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.03, Url = "http://img.geilicdn.com/taobao19779004225_146x150.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.57, Url = "http://img.geilicdn.com/taobao15865634518_146x229.jpg" });
            list.Add(new ImageModel { ImgRatio = 0.99, Url = "http://img.geilicdn.com/taobao15865434962_146x144.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao15830113145_146x219.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.15, Url = "http://img.geilicdn.com/taobao17077543430_146x167.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.46, Url = "http://img.geilicdn.com/taobao19726272617_146x213.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.12, Url = "http://img.geilicdn.com/taobao15865557654_146x163.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.38, Url = "http://img.geilicdn.com/taobao19776424457_146x201.jpg" });
            list.Add(new ImageModel { ImgRatio = 0.99, Url = "http://img.geilicdn.com/taobao19712332041_146x144.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.00, Url = "http://img.geilicdn.com/taobao19722332410_146x146.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao15851750545_146x219.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.35, Url = "http://img.geilicdn.com/taobao15857918167_146x197.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.53, Url = "http://img.geilicdn.com/taobao19704356816_146x223.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.33, Url = "http://img.geilicdn.com/taobao19725572516_146x194.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao19725916030_146x219.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.60, Url = "http://img.geilicdn.com/taobao15845742605_146x233.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.41, Url = "http://img.geilicdn.com/taobao15842817755_146x205.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao15835866335_146x219.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.53, Url = "http://img.geilicdn.com/taobao15693786979_146x223.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.33, Url = "http://img.geilicdn.com/taobao17072171509_146x194.jpg" });
            list.Add(new ImageModel { ImgRatio = 0.67, Url = "http://img.geilicdn.com/taobao17072151693_146x97.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.51, Url = "http://img.geilicdn.com/taobao15882494639_146x220.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.49, Url = "http://img.geilicdn.com/taobao19813600740_146x217.jpg" });
            list.ToObservableCollection<ImageModel>(this.Items);
        }

        private void AddToTop()
        {
            List<ImageModel> topList = new List<ImageModel>();
            topList.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao15835866335_146x219.jpg" });
            //topList.Add(new ImageModel { ImgRatio = 1.53, Url = "http://img.geilicdn.com/taobao15693786979_146x223.jpg" });
            //topList.Add(new ImageModel { ImgRatio = 1.33, Url = "http://img.geilicdn.com/taobao17072171509_146x194.jpg" });
            //topList.Add(new ImageModel { ImgRatio = 0.67, Url = "http://img.geilicdn.com/taobao17072151693_146x97.jpg" });
            //topList.Add(new ImageModel { ImgRatio = 1.51, Url = "http://img.geilicdn.com/taobao15882494639_146x220.jpg" });
            //topList.Add(new ImageModel { ImgRatio = 1.49, Url = "http://img.geilicdn.com/taobao19813600740_146x217.jpg" });
            //topList.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao15830113145_146x219.jpg" });
            topList.InserIntoObservableCollection(this.Items);
        }

        /// <summary>
        /// 爆炸删除效果
        /// </summary>
        /// <param name="obj"></param>
        private void ExplodRemoveItem(object obj)
        {
            if (obj != null && obj is ImageModel)
            {
                ImageModel removeModel = (ImageModel)obj;
                //Debug.WriteLine(removeModel.Url);
                this._items.Remove(removeModel);
            }
        }

        private void RefreshExecute()
        {
            this._items.Clear();
            GetItems();
        }

        private void SetToNullExecute()
        {
            this.Items.Clear();
        }

        private void ItemTapExecute(object obj)
        {
            if (obj != null && obj is ImageModel)
            {
                ImageModel model = (ImageModel)obj;
                model.IsSelected = true;
            }
        }

        private void DeleteSelectExecute()
        {
            for (int i = this._items.Count - 1; i >= 0; i--)
            {
                ImageModel model = this._items[i];
                if (model.IsSelected)
                {
                    this._items.Remove(model);
                }
            }
        }

        private void ResetExecute()
        {
            ObservableCollection<ImageModel> aa = new ObservableCollection<ImageModel>();
            aa.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao15866793897_146x219.jpg" });
            aa.Add(new ImageModel { ImgRatio = 1.33, Url = "http://img.geilicdn.com/taobao19765708787_146x194.jpg" });
            aa.Add(new ImageModel { ImgRatio = 1.00, Url = "http://img.geilicdn.com/taobao15848113020_146x146.jpg" });
            this.Items = aa;
        }
        #endregion

        #region Override
        public override void OnNavigatedTo(PhoneApplicationPage currentPage, NavigationEventArgs e)
        {
            if (e.NavigationMode == NavigationMode.Back)
            {
                //恢复信息
                if (currentPage.State.ContainsKey("items") && 0 >= this.Items.Count)
                {
                    this.Items = currentPage.State["items"] as ObservableCollection<ImageModel>;
                }
                currentPage.State.Remove("items");
            }
            else
            {
                FirstGetItems();
            }
        }

        public override void OnNavigatedFrom(PhoneApplicationPage currentPage, NavigationEventArgs e)
        {
            if (e.NavigationMode != NavigationMode.Back)
            {
                currentPage.State["items"] = this._items;
                return;
            }
            GetDataCommand = null;
            AddToTopCommand = null;
            RefreshCommand = null;
            ExplodItemRemoveCommand = null;
            SetToNullCommand = null;
            ItemTapCommand = null;
            DeleteSelectCommand = null;
            ResetCommand = null;
            _items.Clear();
        }
        #endregion
    }
}
